﻿using OpenCvSharp;
using OSGeo.OGR;
using RemoteSensingImageDirectServer.Models.Request.Listener;
using RemoteSensingImageDirectServer.Models.Request.Params.OGC.RasterMap;
using RemoteSensingImageDirectServer.Models.Request.Response;
using RemoteSensingImageDirectServer.Untils.Pool;

namespace RemoteSensingImageDirectServer.Service.Common
{
    public class ParamFormatService : IParamFormatService
    {
        private readonly IConfiguration _configuration;

        public ParamFormatService(IConfiguration configuration)
        {
            this._configuration = configuration;
        }

        public ServiceResult Check(SingleWMTSParams tileParam)
        {
            ServiceResult result = new ServiceResult(0, null);
            string singlePath = GetSinglePath(tileParam.Layer, tileParam);
            if (!File.Exists(singlePath))
            {
                result.Code = -1;
                result.Message = $"The layer {tileParam.Layer} does not exists.";
            }

            var  coordinateSystem = tileParam.Srs ?? tileParam.TileMatrixset;
            if (!OsrTransformPool.Instance().ContainsEPSG(coordinateSystem)) 
            {
                result.Code = -1;
                result.Message = $"The coordinate {coordinateSystem} system does not exists.";
            }
            return result;
        }

        public ServiceResult Check(GroupWMTSParams tileParam)
        {
            ServiceResult result = new ServiceResult(0, null);
            string groupPath = GetGroupPath(tileParam.Layer);
            if (!Directory.Exists(groupPath))
            {
                result.Code = -1;
                result.Message = $"The layer {tileParam.Layer} does not exists.";
            }

            var coordinateSystem = tileParam.Srs ?? tileParam.TileMatrixset;
            if (!OsrTransformPool.Instance().ContainsEPSG(coordinateSystem))
            {
                result.Code = -1;
                result.Message = $"The coordinate {coordinateSystem} system does not exists.";
            }
            return result;
        }

        public ServiceResult Check(SingleAdWMTSParams tileParam)
        {
            ServiceResult result = new ServiceResult(0, null);
            string singlePath = GetSinglePath(tileParam.Layer, tileParam);
            if (!File.Exists(singlePath))
            {
                result.Code = -1;
                result.Message = $"The layer {tileParam.Layer} does not exists.";
            }

            var coordinateSystem = tileParam.Srs ?? tileParam.TileMatrixset;
            if (!OsrTransformPool.Instance().ContainsEPSG(coordinateSystem))
            {
                result.Code = -1;
                result.Message = $"The coordinate {coordinateSystem} system does not exists.";
            }
            return result;
        }

        public ServiceResult Check(SingleWMSParams tileParam)
        {
            ServiceResult result = new ServiceResult(0, null);
            string singlePath = GetSinglePath(tileParam.Layers, tileParam);
            if (!File.Exists(singlePath))
            {
                result.Code = -1;
                result.Message = $"The layer {tileParam.Layers} does not exists.";
            }

            var coordinateSystem = tileParam.Srs ?? tileParam.Crs;
            if (!OsrTransformPool.Instance().ContainsEPSG(coordinateSystem))
            {
                result.Code = -1;
                result.Message = $"The coordinate {coordinateSystem} system does not exists.";
            }
            return result;
        }

        public ServiceResult Check(GroupWMSParams tileParam)
        {
            ServiceResult result = new ServiceResult(0, null);
            string groupPath = GetGroupPath(tileParam.Layers);
            if (!Directory.Exists(groupPath))
            {
                result.Code = -1;
                result.Message = $"The layer {tileParam.Layers} does not exists.";
            }

            var coordinateSystem = tileParam.Srs ?? tileParam.Crs;
            if (!OsrTransformPool.Instance().ContainsEPSG(coordinateSystem))
            {
                result.Code = -1;
                result.Message = $"The coordinate {coordinateSystem} system does not exists.";
            }
            return result;
        }

        public ServiceResult Check(SingleAdWMSParams tileParam)
        {
            ServiceResult result = new ServiceResult(0, null);
            string singlePath = GetSinglePath(tileParam.Layers, tileParam);
            if (!File.Exists(singlePath))
            {
                result.Code = -1;
                result.Message = $"The layer {tileParam.Layers} does not exists.";
            }

            var coordinateSystem = tileParam.Srs ?? tileParam.Crs;
            if (!OsrTransformPool.Instance().ContainsEPSG(coordinateSystem))
            {
                result.Code = -1;
                result.Message = $"The coordinate {coordinateSystem} system does not exists.";
            }
            return result;
        }

        public string GetSinglePath(string layerName, IRasterListener rasterListener)
        {
            if (rasterListener.IsFullPath)
            {
                return layerName;
            }
            else
            {
                if (layerName.Contains("http") || layerName.Contains("https"))
                {
                    return layerName;
                }
                else
                {
                    return string.Format(@"{0}\{1}", _configuration.GetValue<string>("CacheConfig:SinglePath"), layerName);
                }
            }
        }

        public string GetGroupPath(string layerName)
        {
            return string.Format(@"{0}\{1}", _configuration.GetValue<string>("CacheConfig:GroupPath"), layerName); ;
        }
    }
}
